package dtrs.corba.server.semaphores;

import java.util.HashMap;
import java.util.Map;

import dtrs.corba.server.common.ShowDTO;
import dtrs.corba.utilities.DtrsConstants;


public class DtrsMapShowID {
	
	private Map<String,ShowDTO> mapShows = new HashMap<String,ShowDTO>();
	
	private Map<String,Boolean> indLocksRows = new HashMap<String, Boolean>();
	
	private int ticketsAvailables;
	private boolean indSuccessOper = true;
	private String nameOffice;
	
	
	public DtrsMapShowID(){		

	}
	
	public DtrsMapShowID(Map<String,ShowDTO> mapP){		
		mapShows.putAll(mapP);
	
	}
	
	 
	 
	 /*
	   * In get is necessary lock the row, thus we use synchronized
	   */
	  public synchronized void getTicketsLeftShowId(String keyMap, Integer ticketsLeft, ShowDTO showiP) {
		    
		  try{
			  
			   while(indLocksRows.size()>0 && indLocksRows.get(keyMap)){
			    	wait();
			    }
			    indLocksRows.put(keyMap, true);
			    notifyAll();	
			    Integer valueLeft= ((ShowDTO)(mapShows.get(keyMap))).getNumberOfTicketsLeft();
			    showiP.setNumberOfTicketsLeft(valueLeft);
		       
			  
		  } catch(InterruptedException e){
			  e.printStackTrace();
		  }

	    }
	  
	  
	  /**
	   * In case modify the numberOfTicketsLeft, adding or canceling
	   * @param keyMapShowP
	   * @param newShowRecordP
	   * @return
	   */
	  public synchronized void updateRowShowId(String keyMapShowP, Integer numberOfTicketsP,
			  										int operation, ShowDTO showiP) {
	     
		  try{
			  while (indLocksRows.size()>0 && indLocksRows.get(keyMapShowP)){
				  wait();
			  }

			  indLocksRows.put(keyMapShowP, true);
			  notifyAll();	
			  ShowDTO showIdObj = mapShows.get(keyMapShowP);
			  
			  showiP.setSuccessOper(true);
			  
			  if(operation == DtrsConstants.ROW_ACTION_UPDATE_RESERVE_TICKETS){		
				  if (showIdObj.getNumberOfTicketsLeft()>0 &&
						  numberOfTicketsP <= showIdObj.getNumberOfTicketsLeft() ){
					  showIdObj.setNumberOfTicketsLeft(showIdObj.getNumberOfTicketsLeft() - numberOfTicketsP);
				  }else{
					  showiP.setSuccessOper(false); 
					  
					  if(showIdObj.getNumberOfTicketsLeft()==0){
						  showiP.setNumberOfTicketsLeft(0); // not more tickets available
					  }else	{
						  showiP.setNumberOfTicketsLeft(999);//means desired tickets are bigger that tickets availables 
					  }
					  
				  }
				  
			  }else if(operation ==DtrsConstants.ROW_ACTION_UPDATE_CANCEL_TICKETS){
				  if( showIdObj.getNumberOfTicketsLeft() + numberOfTicketsP <= showIdObj.getNumberOfTickets()){
					  showIdObj.setNumberOfTicketsLeft(showIdObj.getNumberOfTicketsLeft() + numberOfTicketsP);
				  }else {
					  //nothing because maybe there are two threads trying to cancel at same time and the first one complet the numberOfTickets
				  }  
			  }
				  			  
			  mapShows.put(keyMapShowP, showIdObj);			  
			  
		  }catch(InterruptedException e){
			  e.printStackTrace();
		  }

		}
	  
	  
	  
	  
	  
	  
	  /**
	   * Release the lock for the showId
	   * @param keyMapShowP
	   */
	  public synchronized void releaseRow(String keyMapShowP){
		  
		  if(indLocksRows.size()>0 && indLocksRows.get(keyMapShowP)){
				indLocksRows.remove(keyMapShowP);
				notifyAll();
		  }
	  }
	  
	 //----------------------getters-setters
	  
	  

		public int getTicketsAvailables() {
			return ticketsAvailables;
		}



		public Map<String, ShowDTO> getMapShows() {
			return mapShows;
		}



		public void setMapShows(Map<String, ShowDTO> mapShows) {
			this.mapShows = mapShows;
		}



		public void setTicketsAvailables(int ticketsAvailables) {
			this.ticketsAvailables = ticketsAvailables;
		}



		public boolean isIndSuccessOper() {
			return indSuccessOper;
		}



		public void setIndSuccessOper(boolean indSuccessOper) {
			this.indSuccessOper = indSuccessOper;
		}

		public String getNameOffice() {
			return nameOffice;
		}

		public void setNameOffice(String nameOffice) {
			this.nameOffice = nameOffice;
		}
	
		
		

}
